package top.ffhome.doc.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2 配置类
 * @author John
 *
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {
	@Value("${swagger.enable:true}")
	private boolean enableSwagger;
	@Value("${swagger.basePackage:top.ffhome}")
	private String basePackage;
	@Value("${swagger.title:飞飞博客项目}")
	private String title;
	@Value("${swagger.description:}")
	private String description;
	@Value("${swagger.termsOfServiceUrl:https://api.blog.ffhome.top}")
	private String termsOfServiceUrl;
	@Value("${swagger.version:1.0}")
	private String version;
	@Value("${swagger.contact.name:飞飞的技术博客}")
	private String name;
	@Value("${swagger.contact.url:http://blog.ffhome.top}")
	private String url;
	@Value("${swagger.contact.email:58248450@qq.com}")
	private String email;

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enableSwagger).select()
				.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
				.contact(new Contact(name, url, email)).version(version).build();
	}
}
